import java.util.LinkedList;
import java.util.Queue;

public class MyTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    private static int nodeCount;
    // 获取树中节点的个数
    int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        nodeCount++;
        size(root.left);//对所有左数递归
        size(root.right);//对所有右数递归
        return nodeCount;
    }

    /**
     * 子问题求解节点个数
     * @param root
     * @return
     */
    int size2(TreeNode root){
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    // 获取叶子节点的个数
    private static int leafSize = 0;
    int getLeafNodeCount(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }

    // 子问题思路-求叶子结点个数
    int getLeafNodeCount2(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right, k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return (leftH > rightH ? leftH : rightH) + 1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftN = find(root.left, val);
        if (leftN != null) {
            return leftN;
        }
        TreeNode rightN = find(root.right, val);
        if (rightN != null) {
            return rightN;
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.println(top.val);
            if (top.left != null) {
                queue.offer(top.left);
            }
            if (top.right != null) {
                queue.offer(top.right);
            }
        }
    }
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if (top != null) {
                queue.offer(top.left);
                queue.offer(top.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if (top != null) {
                return false;
            }
        }
        return true;
    }
}
